#include "danci_form.h"
#include "ui_danci_form.h"
#include <QDebug>
static int db_temp =0;
database::database()//注意多数据库的打开方式
{
    //在多数据库文件的时候需要这样写，不然会有数据问题
    if(QSqlDatabase::contains("qt_sql_default_connection"))
        db = QSqlDatabase::database("qt_sql_default_connection");
    else
        db=QSqlDatabase::addDatabase("QSQLITE");

}
QSqlDatabase database::initDatabase(QString name)
{

    db.setDatabaseName(name);               //设置数据库名字

    if(!db.open())              //如果打开数据库失败
    {
        QMessageBox msgBox;             //新建MEsssageBox
        msgBox.setText("文件打开错误，请重新尝试。");                    //MessageBox设置文字
    }
    return db;   //返回db数据库
}

void database::closedb()
{
    db.close();             //关闭数据库
}
Danci_Form::Danci_Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Danci_Form)
{
    ui->setupUi(this);
    ui->pushButton_1->setStyleSheet("QPushButton{border-image:url(:/Word/Images/Word/beidanci.png)}QPushButton:hover{border-image:url(:/Word/Images/Word/beidancion.png)}QPushButton:pressed{border-image:url(:/Word/Images/Word/beidanci.png)}");       //按钮美化
    ui->pushButton_2->setStyleSheet("QPushButton{border-image:url(:/Word/Images/Word/jiance.png)}QPushButton:hover{border-image:url(:/Word/Images/Word/jianceon.png)}QPushButton:pressed{border-image:url(:/Word/Images/Word/jiance.png)}");            //按钮美化
    ui->pushButton_3->setStyleSheet("QPushButton{border-image:url(:/Word/Images/Word/shengciben.png)}QPushButton:hover{border-image:url(:/Word/Images/Word/shengcibenon.png)}QPushButton:pressed{border-image:url(:/Word/Images/Word/shengciben.png)}");        //按钮美化
    ui->pushButton_close->setStyleSheet("QPushButton{border-image:url(:/Book/Images/Book/Close.png)}QPushButton:hover{border-image:url(:/Book/Images/Book/Closeon.png)}QPushButton:pressed{border-image:url(:/Book/Images/Book/Close.png)}");       //按钮美化
    ui->pushButton_4->setStyleSheet("QPushButton{border-image:url(:/Word/Images/Word/next.png)}QPushButton:hover{border-image:url(:/Word/Images/Word/nexton.png)}QPushButton:pressed{border-image:url(:/Word/Images/Word/next.png)}");                              //按钮美化
    ui->pushButton_5->setStyleSheet("QPushButton{border-image:url(:/Word/Images/Word/jiarushengciben.png)}QPushButton:hover{border-image:url(:/Word/Images/Word/jiarushengcibenon.png)}QPushButton:pressed{border-image:url(:/Word/Images/Word/jiarushengciben.png)}");     //按钮美化
    ui->pushButton_6->setStyleSheet("QPushButton{border-image:url(:/Word/Images/Word/next.png)}QPushButton:hover{border-image:url(:/Word/Images/Word/nexton.png)}QPushButton:pressed{border-image:url(:/Word/Images/Word/next.png)}");                              //按钮美化
    ui->pushButton_7->setStyleSheet("QPushButton{border-image:url(:/Word/Images/Word/jiarushengciben.png)}QPushButton:hover{border-image:url(:/Word/Images/Word/jiarushengcibenon.png)}QPushButton:pressed{border-image:url(:/Word/Images/Word/jiarushengciben.png)}");     //按钮美化
    shengciben = dbOpen.initDatabase("shengci.db");             //打开数据库
    shengci = new QSqlQuery(shengciben);            //新建数据库查询
    model = new QSqlTableModel(this);               //新建数据库TabModel
    model->setTable("Word");                //设置数据库modle查询
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);         //设置编辑策略
    model->setHeaderData(0,Qt::Horizontal,tr("word"));
    model->setHeaderData(1,Qt::Horizontal,tr("test"));
    model->select();                //执行查询操作
    ui->tableView->setModel(model);             //设置tablview的模型
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->widget_danci->move(70,140);             //移动坐标
    ui->widget_jiance->move(70,140);            //移动坐标
    ui->widget_shengci->move(70,140);           //移动坐标
    ui->widget_danci->hide();           //界面隐藏
    ui->widget_jiance->hide();          //界面隐藏
    ui->widget_shengci->hide();         //界面隐藏
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(post_word_monet()));




}


Danci_Form::~Danci_Form()
{
    delete ui;
}
/*
 * 关闭按钮槽函数
 */
void Danci_Form::on_pushButton_close_clicked()
{
    emit close_word();   //触发关闭单词界面槽函数
    this->close();              //调用关闭函数
}
/*
 * 下一个单词
 */
void Danci_Form::on_pushButton_4_clicked()
{
    switch (db_temp) {
    case 0:
    {
        if(query_select_word->next())           //取下一个单词
        {
            ui->label_word->setText(query_select_word->value(0).toString());                //单词赋值
            ui->label_explain->setText(query_select_word->value(1).toString());             //词义赋值

        }
        break;
    }
    case 1:
    {
        if(query_select_word_test->next())             //取下一条数据
        {
            ui->label_word_2->setText(query_select_word_test->value(0).toString());             //赋值
            ui->pushButton_11->setText(query_select_word_test->value(1).toString());//赋值
            ui->pushButton_10->setText(query_select_word_test->value(2).toString());//赋值
            ui->pushButton_9->setText(query_select_word_test->value(3).toString());//赋值
            ui->pushButton_8->setText(query_select_word_test->value(4).toString());//赋值
            word_exp = query_select_word_test->value(5).toString();//赋值

        }
    }
    default:
        break;
    }
}
/*
 * 被单词启动按钮
 */
void Danci_Form::on_pushButton_1_clicked()
{
    ui->widget_jiance->hide();
    ui->widget_shengci->hide();
    ui->widget_danci->show();
    word_Date = dbOpen.initDatabase("Word_Date.db");                //打开数据库文件
    QSqlQuery query(word_Date);
    query.exec("select name from sqlite_master where type='table'order by name");                   //查询数据库所有列表
    ui->comboBox->clear();
    while (query.next()) {
        /*
         * 查询数据库中所有列表
         * 将所抱有的表名添加到combox中
         */

        ui->comboBox->addItem(query.value(0).toString());

    }
    db_temp =0;
}

void Danci_Form::on_comboBox_activated(const QString &arg1)
{
    switch (db_temp) {
    case 0:
    {
        QString sql_select_word = "select * from "+arg1;                    //新建查询语句
        query_select_word=new QSqlQuery(word_Date);             //新建查询
        query_select_word->exec(sql_select_word);           //执行查选语句
        if(query_select_word->first())          //获取第一条记录
        {
            ui->label_word->setText(query_select_word->value(0).toString());    //将第一条记录的第一个参数复制到lable上面
            ui->label_explain->setText(query_select_word->value(1).toString());     //将第二个记录的参数赋值到label上面
        }
        break;          //返回
    }
    case 1:
    {
        QString sql_select_word_test = "select * from "+arg1;    //新建查询语句
        query_select_word_test=new QSqlQuery(word_Date_Test);               //新建查询
        query_select_word_test->exec(sql_select_word_test);             //执行查询语句
        if(query_select_word_test->first())             //去第一条数据
        {
            ui->label_word_2->setText(query_select_word_test->value(0).toString());     //赋值
            ui->pushButton_11->setText(query_select_word_test->value(1).toString());//赋值
            ui->pushButton_10->setText(query_select_word_test->value(2).toString());//赋值
            ui->pushButton_9->setText(query_select_word_test->value(3).toString());//赋值
            ui->pushButton_8->setText(query_select_word_test->value(4).toString());//赋值
            word_exp = query_select_word_test->value(5).toString();//赋值

        }

        break;
    }
    default:
        break;
    }
}

void Danci_Form::on_pushButton_2_clicked()
{
    ui->widget_danci->hide();
    ui->widget_shengci->hide();
    ui->widget_jiance->show();
    word_Date_Test = dbOpen.initDatabase("Word_Date.db");                //打开数据库文件
    QSqlQuery query(word_Date_Test);
    query.exec("select name from sqlite_master where type='table'order by name");                   //查询数据库所有列表
    ui->comboBox->clear();
    while (query.next()) {
        /*
         * 查询数据库中所有列表
         * 将所抱有的表名添加到combox中
         */

        ui->comboBox->addItem(query.value(0).toString());

    }
    db_temp =1;
}

void Danci_Form::on_pushButton_6_clicked()
{
    switch (db_temp) {
    case 0:
    {
        if(query_select_word->next())           //取下一个单词
        {
            ui->label_word->setText(query_select_word->value(0).toString());                //单词赋值
            ui->label_explain->setText(query_select_word->value(1).toString());             //词义赋值

        }
        break;
    }
    case 1:
    {
        if(query_select_word_test->next())             //取下一条数据
        {
            ui->label_word_2->setText(query_select_word_test->value(0).toString());         //赋值
            ui->pushButton_11->setText(query_select_word_test->value(1).toString());//赋值
            ui->pushButton_10->setText(query_select_word_test->value(2).toString());//赋值
            ui->pushButton_9->setText(query_select_word_test->value(3).toString());//赋值
            ui->pushButton_8->setText(query_select_word_test->value(4).toString());//赋值
            word_exp = query_select_word_test->value(5).toString();//赋值

        }
    }
    default:
        break;
    }
}

static int the_test_word_flag = 0;
void Danci_Form::on_pushButton_11_clicked()
{
    if(ui->pushButton_11->text() == word_exp)
    {
        if(query_select_word_test->next())             //取下一条数据
        {
            the_test_word_flag = 0;
            ui->label_word_2->setText(query_select_word_test->value(0).toString());                 //赋值
            ui->pushButton_11->setText(query_select_word_test->value(1).toString());//赋值
            ui->pushButton_10->setText(query_select_word_test->value(2).toString());//赋值
            ui->pushButton_9->setText(query_select_word_test->value(3).toString());//赋值
            ui->pushButton_8->setText(query_select_word_test->value(4).toString());//赋值
            word_exp = query_select_word_test->value(5).toString();//赋值

        }
        if(the_test_word_flag ==0)
        {
            emit word_money(31);
        }
    }
    else
    {
        the_test_word_flag++;
        int ok = QMessageBox::warning(this,"警告","选择错误\n是否加入生词本",QMessageBox::Yes,QMessageBox::No);
        if(ok == QMessageBox::Yes)
        {
            int temp = model->rowCount();
            model->insertRow(temp);
            model->setData(model->index(temp,0),ui->label_word_2->text());
            model->setData(model->index(temp,1),word_exp);
            model->submitAll();
            if(query_select_word_test->next())             //取下一条数据
            {
                the_test_word_flag = 0;
                ui->label_word_2->setText(query_select_word_test->value(0).toString());                 //赋值
                ui->pushButton_11->setText(query_select_word_test->value(1).toString());//赋值
                ui->pushButton_10->setText(query_select_word_test->value(2).toString());//赋值
                ui->pushButton_9->setText(query_select_word_test->value(3).toString());//赋值
                ui->pushButton_8->setText(query_select_word_test->value(4).toString());//赋值
                word_exp = query_select_word_test->value(5).toString();//赋值

            }
        }
    }
}

void Danci_Form::on_pushButton_9_clicked()
{
    if(ui->pushButton_10->text() == word_exp)
    {
        if(query_select_word_test->next())             //取下一条数据
        {
            the_test_word_flag = 0;
            ui->label_word_2->setText(query_select_word_test->value(0).toString());         //赋值
            ui->pushButton_11->setText(query_select_word_test->value(1).toString());//赋值
            ui->pushButton_10->setText(query_select_word_test->value(2).toString());//赋值
            ui->pushButton_9->setText(query_select_word_test->value(3).toString());//赋值
            ui->pushButton_8->setText(query_select_word_test->value(4).toString());//赋值
            word_exp = query_select_word_test->value(5).toString();//赋值

        }
        if(the_test_word_flag ==0)
        {
            emit word_money(31);
        }
    }
    else
    {
        the_test_word_flag++;
        int ok = QMessageBox::warning(this,"警告","选择错误\n是否加入生词本",QMessageBox::Yes,QMessageBox::No);
        if(ok == QMessageBox::Yes)
        {
            int temp = model->rowCount();
            model->insertRow(temp);
            model->setData(model->index(temp,0),ui->label_word_2->text());
            model->setData(model->index(temp,1),word_exp);
            model->submitAll();
            if(query_select_word_test->next())             //取下一条数据
            {
                the_test_word_flag = 0;
                ui->label_word_2->setText(query_select_word_test->value(0).toString());                 //赋值
                ui->pushButton_11->setText(query_select_word_test->value(1).toString());//赋值
                ui->pushButton_10->setText(query_select_word_test->value(2).toString());//赋值
                ui->pushButton_9->setText(query_select_word_test->value(3).toString());//赋值
                ui->pushButton_8->setText(query_select_word_test->value(4).toString());//赋值
                word_exp = query_select_word_test->value(5).toString();//赋值

            }
        }
    }
}

void Danci_Form::on_pushButton_8_clicked()
{
    if(ui->pushButton_9->text() == word_exp)
    {
        if(query_select_word_test->next())             //取下一条数据
        {
            the_test_word_flag = 0;
            ui->label_word_2->setText(query_select_word_test->value(0).toString());             //赋值
            ui->pushButton_11->setText(query_select_word_test->value(1).toString());//赋值
            ui->pushButton_10->setText(query_select_word_test->value(2).toString());//赋值
            ui->pushButton_9->setText(query_select_word_test->value(3).toString());//赋值
            ui->pushButton_8->setText(query_select_word_test->value(4).toString());//赋值
            word_exp = query_select_word_test->value(5).toString();//赋值

        }
        if(the_test_word_flag ==0)
        {
            emit word_money(31);
        }
    }
    else
    {
        the_test_word_flag++;
        int ok = QMessageBox::warning(this,"警告","选择错误\n是否加入生词本",QMessageBox::Yes,QMessageBox::No);
        if(ok == QMessageBox::Yes)
        {
            the_test_word_flag = 0;
            int temp = model->rowCount();
            model->insertRow(temp);
            model->setData(model->index(temp,0),ui->label_word_2->text());
            model->setData(model->index(temp,1),word_exp);
            model->submitAll();
            if(query_select_word_test->next())             //取下一条数据
            {
                ui->label_word_2->setText(query_select_word_test->value(0).toString());                 //赋值
                ui->pushButton_11->setText(query_select_word_test->value(1).toString());//赋值
                ui->pushButton_10->setText(query_select_word_test->value(2).toString());//赋值
                ui->pushButton_9->setText(query_select_word_test->value(3).toString());//赋值
                ui->pushButton_8->setText(query_select_word_test->value(4).toString());//赋值
                word_exp = query_select_word_test->value(5).toString();//赋值

            }
        }
    }
}

void Danci_Form::on_pushButton_10_clicked()
{
    if(ui->pushButton_8->text() == word_exp)
    {
        if(query_select_word_test->next())             //取下一条数据
        {
            the_test_word_flag = 0;
            ui->label_word_2->setText(query_select_word_test->value(0).toString());             //赋值
            ui->pushButton_11->setText(query_select_word_test->value(1).toString());//赋值
            ui->pushButton_10->setText(query_select_word_test->value(2).toString());//赋值
            ui->pushButton_9->setText(query_select_word_test->value(3).toString());//赋值
            ui->pushButton_8->setText(query_select_word_test->value(4).toString());//赋值
            word_exp = query_select_word_test->value(5).toString();//赋值

        }
        if(the_test_word_flag ==0)
        {
            emit word_money(31);
        }
    }
    else
    {
        the_test_word_flag++;
        int ok = QMessageBox::warning(this,"警告","选择错误\n是否加入生词本",QMessageBox::Yes,QMessageBox::No);
        if(ok == QMessageBox::Yes)
        {
            the_test_word_flag = 0;
            int temp = model->rowCount();
            model->insertRow(temp);
            model->setData(model->index(temp,0),ui->label_word_2->text());
            model->setData(model->index(temp,1),word_exp);
            model->submitAll();
            if(query_select_word_test->next())             //取下一条数据
            {
                ui->label_word_2->setText(query_select_word_test->value(0).toString());                 //赋值
                ui->pushButton_11->setText(query_select_word_test->value(1).toString());//赋值
                ui->pushButton_10->setText(query_select_word_test->value(2).toString());//赋值
                ui->pushButton_9->setText(query_select_word_test->value(3).toString());//赋值
                ui->pushButton_8->setText(query_select_word_test->value(4).toString());//赋值
                word_exp = query_select_word_test->value(5).toString();//赋值

            }
        }
    }
}

void Danci_Form::on_pushButton_3_clicked()
{
    ui->widget_danci->hide();
    ui->widget_jiance->hide();
    ui->widget_shengci->show();
}

void Danci_Form::on_pushButton_13_clicked()
{

    int ok = QMessageBox::warning(this,"删除当前行","确定删除该生词吗",QMessageBox::Yes,QMessageBox::No);
    if(ok == QMessageBox::No)
    {

        model->revertAll();                     //回滚

    }
    else
    {
        int curRow = ui->tableView->currentIndex().row();
        model->removeRow(curRow);
        model->submitAll();             //提交数据库操作
    }
}

void Danci_Form::on_pushButton_5_clicked()
{
    int temp = model->rowCount();
    model->insertRow(temp);
    model->setData(model->index(temp,0),ui->label_word->text());
    model->setData(model->index(temp,1),ui->label_explain->text());
    model->submitAll();
    qDebug()<<"sdsfdsfsdfdsfdsfdsfdsfds";
    if(query_select_word->next())           //取下一个单词
    {
        ui->label_word->setText(query_select_word->value(0).toString());                //单词赋值
        ui->label_explain->setText(query_select_word->value(1).toString());             //词义赋值

    }
}
/*
 * 被单次增肌金币函数
 * 作废
 */
void Danci_Form::post_word_monet()
{
    emit word_money(31);
}

void Danci_Form::on_widget_shengci_destroyed()
{

}

void Danci_Form::on_widget_shengci_destroyed(QObject *arg1)
{

}
